home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Source Code / Visual Basic Source Code.iso / vbsource / wrcmdd / winio.c < prev    next >
C/C++ Source or Header  |  1995-02-28  |  33KB  |  967 lines

  1. /*
  2. WINIO.C
  3. Stdio (e.g. printf) functionality for Windows - implementation
  4. Dave Maxey - 1991
  5. */
  6.  
  7. #include <windows.h>
  8. #include <stdlib.h>
  9. #include <stdarg.h>
  10. #include <malloc.h>
  11. #include <string.h>
  12. #include "wmhandlr.h"
  13. #include "winio.h"
  14.  
  15. /* PROTOTYPES in alphabetic order */
  16.  
  17. void            addchars(char *, unsigned);
  18. void            adjust_caret(void);
  19. void            append2buffer(char *, unsigned);
  20. int             chInput(void);
  21. void            compute_repaint(void);
  22. int             initialize_buffers(unsigned);
  23. int             initialize_class(HANDLE);
  24. void            initialize_state(void);
  25. int             initialize_window(HANDLE, HANDLE, int);
  26. void            make_avail(unsigned);
  27. char far *      nextline(char far *);
  28. char far *      prevline(char far *);
  29. void            set_font(void);
  30. long            winio_wmpaint(HWND, unsigned, WORD, LONG);
  31. long            winio_wmsize(HWND, unsigned, WORD, LONG);
  32. long            winio_wmdestroy(HWND, unsigned, WORD, LONG);
  33. long            winio_wmchar(HWND, unsigned, WORD, LONG);
  34. long            winio_wmkeydown(HWND, unsigned, WORD, LONG);
  35. long            winio_wmhscroll(HWND, unsigned, WORD, LONG);
  36. long            winio_wmvscroll(HWND, unsigned, WORD, LONG);
  37. long            winio_wmsetfocus(HWND, unsigned, WORD, LONG);
  38. long            winio_wmkillfocus(HWND, unsigned, WORD, LONG);
  39.  
  40. // this doesn't get declared in stdio.h if _WINDOWS is defined
  41. // although it is in the Windows libraries!
  42. int             vsprintf(char *, const char *, va_list);
  43.  
  44. #define         winio_caret_visible() \
  45.                 ((yCurrLine <= (yTopOfWin + yWinHeight)) && \
  46.                 (xCurrPos <= (xLeftOfWin + xWinWidth)) && \
  47.                 (xCurrPos >= xLeftOfWin))
  48.  
  49. #define         CHECK_INIT() if (! tWinioVisible) return FALSE
  50.                  
  51. #define         MAX_X                   127
  52. #define         TABSIZE                 8
  53. #define         TYPE_AHEAD              256
  54. #define         WINIO_DEFAULT_BUFFER    8192
  55. #define         MIN_DISCARD             256
  56. #define         CARET_WIDTH             2
  57.  
  58. // For scrolling procedures
  59. #define         USE_PARAM               10000
  60. #define         DO_NOTHING              10001
  61.  
  62. char            winio_class[15] = "winio_class";
  63. char            winio_icon[15] = "winio_icon";
  64. char            winio_title[128] = "Console I/O";
  65. unsigned long   bufsize = WINIO_DEFAULT_BUFFER;
  66. unsigned long   kbsize = TYPE_AHEAD;
  67. int             bufused, bufSOI;
  68. int             curr_font = SYSTEM_FIXED_FONT;
  69. int             tWinioVisible = FALSE;
  70. int             tCaret = FALSE, tFirstTime = TRUE;
  71. int             cxChar, cyChar, cxScroll, cyScroll, cxWidth, cyHeight;
  72. int             xWinWidth, yWinHeight, xCurrPos;
  73. int             xLeftOfWin, yTopOfWin, yCurrLine;
  74. unsigned        pchKbIn, pchKbOut;
  75. static char far *fpBuffer, far *fpTopOfWin, far *fpCurrLine; 
  76. static char far *fpKeyboard;
  77. static HANDLE   hBuffer, hKeyboard;
  78. HWND         hWnd;
  79. BOOL            tTerminate = TRUE,
  80.                 tPaint = TRUE;
  81. DESTROY_FUNC    destroy_func;
  82.  
  83. typedef struct {
  84.     int hSB, vSB;
  85.     } recVKtoSB;
  86.                 
  87. /* This table defines, by scroll message, what increment to try */
  88. /* and scroll horizontally. PGUP and PGDN entries are updated   */
  89. /* in the winio_wmsize function.                                */
  90. int             cScrollLR[SB_ENDSCROLL + 1] =
  91. //UP  DOWN PGUP     PGDN    POS        TRACK      TOP     BOT    ENDSCROLL
  92. { -1, +1,  -1,      +1,     USE_PARAM, USE_PARAM, -MAX_X, MAX_X, DO_NOTHING};
  93.                 
  94. /* This table defines, by scroll message, what increment to try */
  95. /* and scroll horizontally. PGUP and PGDN entries are updated   */
  96. /* in the winio_wmsize function, and the TOP & BOTTOM entries   */
  97. /* are updated by addchar function.                             */
  98. int             cScrollUD[SB_ENDSCROLL + 1] =
  99. //UP  DOWN PGUP     PGDN    POS        TRACK      TOP     BOT    ENDSCROLL
  100. { -1, +1,  -1,      +1,     USE_PARAM, USE_PARAM, -1,     +1,    DO_NOTHING};
  101.                 
  102. /* This table associates horizontal and vertical scroll         */
  103. /* messages that should be generated by the arrow and page keys */
  104. recVKtoSB       VKtoSB[VK_DOWN - VK_PRIOR + 1] =
  105. //                  VK_PRIOR                    VK_NEXT
  106.                 {   { DO_NOTHING, SB_PAGEUP },  { DO_NOTHING, SB_PAGEDOWN },
  107. //                  VK_END                      VK_HOME
  108.                     { SB_TOP, SB_BOTTOM },      { SB_TOP, SB_TOP },
  109. //                  VK_LEFT                     VK_UP
  110.                     { SB_LINEUP, DO_NOTHING },  { DO_NOTHING, SB_LINEUP },
  111. //                  VK_RIGHT                    VK_DOWN
  112.                     { SB_LINEDOWN, DO_NOTHING },{ DO_NOTHING, SB_LINEDOWN } };
  113.                 
  114. /* ===================================================================  */
  115. /* the interface functions themselves.....                              */
  116. /* ===================================================================  */
  117.  
  118. char *gets(char *pchTmp)
  119. {
  120.     char *pch = pchTmp;
  121.     int c;
  122.  
  123.     CHECK_INIT();
  124.     bufSOI = bufused; /* mark beginning of line to limit backspace */
  125.     do {
  126.         switch (c = fgetchar())
  127.             {
  128.             case '\b' :     if (pch > pchTmp) pch--; break;
  129.             case 0x1b :     pch = pchTmp; break;
  130.             case EOF :      bufSOI = -1; return NULL;
  131.             default :       *pch = (char) c; pch++;
  132.             }
  133.         } while (c);
  134.     bufSOI = -1;
  135.     return pchTmp;
  136.     }
  137.  
  138. int printf(const char *fmt, ...)
  139.     {
  140.     va_list marker;
  141.     va_start(marker, fmt);
  142.     return vprintf(fmt, marker);
  143.     }
  144.  
  145. int vprintf(const char *fmt, va_list marker)
  146.     {
  147.     static char s[1024];
  148.     int len;
  149.  
  150.     CHECK_INIT();
  151.     len = vsprintf(s, fmt, marker);
  152.     addchars(s,len);
  153.     return len;
  154.     }
  155.  
  156. int fgetchar(void)
  157.     {
  158.     CHECK_INIT();
  159.     return fputchar(chInput());
  160.     }
  161.  
  162. int kbhit(void)
  163.     {
  164.     CHECK_INIT();
  165.     return (pchKbIn == pchKbOut);
  166.     }
  167.  
  168. int fputchar(int c)
  169.     {
  170.     CHECK_INIT();
  171.     addchars(&((char) c), 1);
  172.     return c;
  173.     }
  174.  
  175. int puts(const char *s)
  176.     {
  177.     char c = '\n';
  178.     CHECK_INIT();
  179.     addchars((char *) s, strlen(s));
  180.     addchars(&c, 1);
  181.     return 0;
  182.     }
  183.  
  184. /* ---------------------------------------------------------------  */
  185. /* USED INTERNALLY - pops up an error window and returns FALSE      */
  186. /* ---------------------------------------------------------------  */
  187. int fail(char *s)
  188.     {
  189.     MessageBox(NULL,s,"ERROR",MB_OK);
  190.     return FALSE;
  191.     }
  192.  
  193. /* ---------------------------------------------------------------  */
  194. /* pops up a message window                                         */
  195. /* ---------------------------------------------------------------  */
  196. BOOL winio_warn(BOOL confirm, const char *fmt, ...)
  197.     {
  198.     char s[256];
  199.     va_list marker;
  200.  
  201.     va_start(marker, fmt);
  202.     vsprintf(s, fmt, marker);
  203.     va_end(marker);
  204.     
  205.     return (MessageBox(NULL, s, winio_title, 
  206.         confirm? MB_OKCANCEL : MB_OK) == IDOK);
  207.     }
  208.  
  209. /* ---------------------------------------------------------------  */
  210. /* The application must call this function before using any of the  */
  211. /* covered stdio type calls. We need the parameter info in order    */
  212. /* to create the window. The function allocates the buffer and      */
  213. /* creates the window. It returns TRUE or FALSE.                    */
  214. /* ---------------------------------------------------------------  */
  215. int winio_init(HANDLE hInstance, HANDLE hPrevInstance,
  216.             int nCmdShow, unsigned wBufSize)
  217.     {
  218.     if (tWinioVisible)
  219.         return FALSE;
  220.     
  221.     if (! initialize_buffers(wBufSize))
  222.         return FALSE;
  223.  
  224.     initialize_state();
  225.     
  226.     if (! initialize_window(hInstance, hPrevInstance, nCmdShow))
  227.         return FALSE;
  228.     
  229.     tWinioVisible = TRUE;
  230.     
  231.     atexit(winio_end);  /* hook into exit chain */
  232.  
  233.     winio_yield();
  234.     return TRUE;
  235.     }
  236.  
  237. /* ---------------------------------------------------------------  */
  238. /* Clear the contents of the buffer.